package com.mywang.weitravel.modules.footprint;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mywang.weitravel.R;
import com.mywang.weitravel.adapter.FootprintListAdapter;
import com.mywang.weitravel.base.BaseFragment;
import com.mywang.weitravel.model.FootprintListBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.BindView;

/**
 * Created by wangmingyun on 2019/12/5.
 */
public class FootPrintFragment extends BaseFragment<FootprintPresenter> implements FootprintContract.View, FootprintListAdapter.MarkListener {

    @BindView(R.id.rv_footprint)
    RecyclerView rvFootprint;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.iv_title_back)
    ImageView ivTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    private FootprintListAdapter listAdapter;

    private int page = 1;

    @Override
    protected FootprintPresenter initPresenter() {
        return new FootprintPresenter(this);
    }

    @Override
    protected int getFragmentLayoutID() {
        return R.layout.fragment_footprint;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        ivTitleBack.setVisibility(View.GONE);
        tvTitle.setText("足迹");
        listAdapter = new FootprintListAdapter(mActivity, this);
        rvFootprint.setLayoutManager(new LinearLayoutManager(mActivity));
        rvFootprint.setAdapter(listAdapter);

        srlLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page++;
                mPresenter.getFootPrintList(page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                mPresenter.getFootPrintList(page);
            }
        });

        mPresenter.getFootPrintList(page);
    }

    @Override
    public void getListSuccess(FootprintListBean footprintListBean) {
        srlLayout.finishRefresh();
        srlLayout.finishLoadmore();
        if (footprintListBean.getList().size() > 0) {
            if (page == 1) {
                listAdapter.refreshData(footprintListBean.getList());
            } else {
                listAdapter.addData(footprintListBean.getList());
            }
        } else {
            showFailed("暂无数据");
        }
    }

    @Override
    public void getListError() {
        srlLayout.finishRefresh();
        srlLayout.finishLoadmore();
    }

    @Override
    public void markFootPrintSuccessful(int position) {
        listAdapter.updateItem(position);
    }

    @Override
    public void Mark(int position, String id) {
        mPresenter.markFootPrint(position, id);
    }
}
